package org.cloudfoundry.samples;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.cloudfoundry.samples.domain.CustomerLocation;
import org.cloudfoundry.samples.domain.CustomerType;
import org.cloudfoundry.samples.domain.TaxiLocation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Handles requests for the application home page.
 */
@Controller
public class LocationController {

	private static final Logger logger = LoggerFactory.getLogger(LocationController.class);

	@Inject
	private ReferenceDataRepository referenceRepository;

	/**
	 * 未注册用户只能查询3分钟内的记录；
	 * 注册用户可以查询5分钟内的记录；
	 * vip用户可以查询15分钟内的记录；
	 * @return
	 */
    @RequestMapping(value = "/listcustomerlocations/{deviceid}", method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody List<CustomerLocation> listPassengerlocations(@PathVariable String deviceid) {
        logger.info("listcustomerlocations...");
        
        int customerLevel = referenceRepository.getCustomerType(deviceid);
        
        return referenceRepository.getPassengerLocations(CustomerType.getMin(customerLevel));
    }
    
    /**
     * 未注册用户只能查询3分钟内的记录；
     * 注册用户可以查询5分钟内的记录；
     * vip用户可以查询15分钟内的记录；
     * @return
     */
    @RequestMapping(value = "/listtaxilocations/{deviceid}", method = RequestMethod.GET, produces = "application/json")
    public @ResponseBody List<TaxiLocation> listtaxilocations(@PathVariable String deviceid) {
        logger.info("listtaxilocations...");
        
        int customerLevel = referenceRepository.getCustomerType(deviceid);
        
        return referenceRepository.getTaxiLocations(CustomerType.getMin(customerLevel));
    }
    
    @RequestMapping(value = "savecustomerlocation", method = RequestMethod.POST)
    public @ResponseBody String savePassengerlocation(@RequestBody LinkedMultiValueMap<String, String> map) {
        logger.info("savecustomerlocation...");
        
        CustomerLocation location = new CustomerLocation();

        location.setAddress(map.getFirst("addr"));
        location.setPhoneNumber(map.getFirst("pn"));
        location.setLat(map.getFirst("lat"));
        location.setLon(map.getFirst("lon"));
        location.setUserId(map.getFirst("customerid"));
        location.setCreateTime(new Date());
        location.setUpdateTime(new Date());
        
        referenceRepository.savePassengerLocation(location);
        
        return "Map message received! Your message: " + location.toString();
    }
    
    @RequestMapping(value = "savetaxilocation", method = RequestMethod.POST)
    public @ResponseBody String savetaxilocation(@RequestBody LinkedMultiValueMap<String, String> map) {
        logger.info("savetaxilocation...");
        
        TaxiLocation location = new TaxiLocation();
        
        location.setAddress(map.getFirst("addr"));
        location.setPhoneNumber(map.getFirst("pn"));
        location.setLat(map.getFirst("lat"));
        location.setLon(map.getFirst("lon"));
        location.setUserId(map.getFirst("taxiid"));
        location.setCreateTime(new Date());
        location.setUpdateTime(new Date());
        
        referenceRepository.saveTaxiLocation(location);
        
        return "Map message received! Your message: " + location.toString();
    }
}
